// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

/**
 * 提现界面
 */
cc.Class({
    extends: cc.Component,

    properties: {
        _selectTg:null,  //单选按钮选中(提现单选按钮)   
        labDec: cc.Label, //金额输入提示 
        edMoney:cc.Label,  //金额输入框
        labBalance:cc.Label,  //可提现金额
        labCont:cc.Label,  //提现次数
        btnNoBankCard:cc.Button, //添加银行卡
        btnExistBankCard:cc.Button,  //存在银行卡
        labBankLastNum:cc.Label, //银行卡后四位数
        inputPwdPre: cc.Prefab,  //输入提现密码预制界面
        numberKeysPre: cc.Prefab,  //键盘界面预制
        bindBankPrefab: cc.Prefab,  //添加银行卡预制

        _isHasCard:false,//是否有银行卡
        _minMoney:10.00,//至少提现多少金额
        _maxCount:0,//最多提现多少次
        _withdrawMoney:0,//当前可提现金额
        _backState: false,
        _keysOpen: false,
        _backExist: false,

    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.initInfo();
        this.getOneCard();
       
        this.cash = "";
        
    },

    init: function(para){
        this._selectTg =para;
    },

    initInfo:function(){
        this.labCont.string = "0次";
         var recv = function(ret){
            ComponentsUtils.unblock();
            if(ret.Code != 0)
            {
                ComponentsUtils.showTips(ret.Msg);
                cc.error(ret.Msg);
            }
            else
            {
                this._minMoney = 10.00;
                this._maxCount = ret.Count;
                //this.labDec1.string = "1.提现金额至少"+ this._minMoney +"元，只可提现中奖奖金";
                //this.labDec2.string = "2.每个账户每天最多提现"+ret.Count + "次";
                this._withdrawMoney = LotteryUtils.moneytoServer(ret.WithdrawMoney);
                this.labBalance.string = this._withdrawMoney +"元宝";
                this.labCont.string = ret.Count + "次";
                if(User.getBalance()>this._minMoney)
                {
                    this.labDec.string = "请输入提现金额";
                }
                else
                {
                    this.labDec.string = "可提现余额不足，无法提现";
                }
            }
        }.bind(this);          

        var data = {
            Token:User.getLoginToken(),
            UserCode:User.getUserCode(),
            b:false
        }
        CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.GETCASHINFO, data, recv.bind(this),"POST");  
        ComponentsUtils.block();
    },

    //前往添加银行卡
    onGotoBankCard: function(){
        var canvas = cc.find("Canvas");
        var bankCard = cc.instantiate(this.bindBankPrefab);
        var Callback = function(ret){
           // cc.log("选择银行卡回调",ret);
            if(ret == null)
            {
                this.getOneCard();
            }
            else
            {
                this.btnExistBankCard.node.active = true;
                this.btnNoBankCard.node.active = false;
                // this.labBankType.string = ret.bankname;
                this.labBankLastNum.string = ret.last;
                this._bankCode = ret.bankcode;
                this._isHasCard = true;
            }
        }.bind(this);
        bankCard.getComponent("user_bankCard_pop").init(1,Callback);
        canvas.addChild(bankCard);
        
    },

    //获取一张银行卡
    getOneCard:function(){
        var recv = function(ret){
            ComponentsUtils.unblock();
            if(ret.Code != 0 && ret.Code != 49)
            {
                ComponentsUtils.showTips(ret.Msg);
                cc.error(ret.Msg);
                this.btnNoBankCard.node.active = true;
                this._isHasCard = false;
            }
            else
            {
                if(ret.Data != null)
                {
                    this.btnExistBankCard.node.active = true;
                    this.btnNoBankCard.node.active = false;
                    //this.labBankType.string = ret.Data[0].BankName;
                    this.labBankLastNum.string = ret.Data[0].CardNum.substr(ret.Data[0].CardNum.length-4);
                    this._bankCode = ret.Data[0].BankCode;
                    this._isHasCard = true;
                }
                else
                {
                    this.btnNoBankCard.node.active = true;
                    this._isHasCard = false;
                }
            }
        }.bind(this);          

        var data = {
            Token:User.getLoginToken(),
            UserCode:User.getUserCode(),
            PageNumber:1,
            RowsPerPage:1,
        }
        CL.HTTP.sendRequestRet(DEFINE.HTTP_MESSAGE.GETBANKCARDLIST, data, recv.bind(this),"POST");   
        ComponentsUtils.block();
    },

    onTextBegan: function(){
        if(!this.node.getChildByName('public_numberKeys_pop')){  //当键盘存在，就不必创建
            this.KeysPre =cc.instantiate(this.numberKeysPre);
            this.node.addChild(this.KeysPre);
            var Callback = function(data){   //键盘输入回调
            this.onTextChangedCash(data);
            }.bind(this);
            this.KeysPre.getComponent(this.KeysPre.name).init(Callback);
            this.KeysPre.getComponent(this.KeysPre.name).onShow();  //显示键盘界面  
        }else{
            this.KeysPre.getComponent(this.KeysPre.name).onShow();  //显示键盘界面      
        }
        this._keysOpen =true;  //键盘开启
    },

    //输入金额时
    onTextChangedCash:function(text){
        var txt = text;
        if( txt == "" )
        {
            this.cash = txt;
        } 
        else
        {
            if(txt =='-'){
                if(this.edMoney.string.length > 0){
                    this.edMoney.string =this.edMoney.string.slice(0,-1);   //清除输入的金额
                }else{
                    if(User.getBalance()>this._minMoney)
                    {
                        this.labDec.string = "请输入提现金额";
                    }
                    else
                    {
                        this.labDec.string = "可提现余额不足，无法提现";
                    }    
                } 
            }else if(txt =='ok'){
                this.KeysPre.getComponent(this.KeysPre.name).onHide();  //隐藏键盘界面   
                
            }else{
                this.cash = txt.replace(/[^\d.]/g,"");
                txt = this.cash;
                this.edMoney.string += txt;
                if(this.edMoney.string.length>8){   //只能输入8位
                    this.edMoney.string =this.edMoney.string.slice(0,8);  
                }
                this.labDec.string ="";
                return;
            }
           
        }
    },

    //点击其他位置，关闭键盘
    onCloseKeys: function(){
        if(!this._keysOpen){
            return;
        }
        if(this.KeysPre){
            this.KeysPre.getComponent(this.KeysPre.name).onHide();  //隐藏键盘界面
            this._keysOpen =false;
        }
    },

    checkInput: function(){
        if(this.edMoney.string != "")
        {
            if(!Utils.isInt(this.edMoney.string) && !Utils.isFolatMoney(this.edMoney.string))
            {
                ComponentsUtils.showTips("输入金额错误");
                return false;
            }
        }
        else
        {
            ComponentsUtils.showTips("输入金额不能为空");
            return false;
        }
        return true;
    },

    onTrue: function(){
        if(!this._isHasCard)
        {
            ComponentsUtils.showTips("请先绑定银行卡");
            return;
        }
        if(this._maxCount<=0)
        {
            ComponentsUtils.showTips("当日提现次数已用完，请明日再试");
            return;
        }
        var money = this.edMoney.string;
        // if(Utils.isFolatMoney(money))
        // {
        //     ComponentsUtils.showTips("输入金额错误");
        //     return;
        // }

        if(this.checkInput())
        {
            var iMoney = parseFloat(money);
            if(this._withdrawMoney<iMoney)
            {
                ComponentsUtils.showTips("输入金额不能大于可提现金额");
                return;
            }
            if(iMoney<this._minMoney)
            {
                ComponentsUtils.showTips("输入金额不能小于"+this._minMoney);
                return;
            }
            // if(iMoney <= User.getBalance())
            // {
            //     ComponentsUtils.showTips("输入金额不能大于当前余额");
            //     return;
            // }
            this._backState =true;  //键盘界面是否打开 

            var canvas =cc.find('Canvas');
            var inputPwdPre =cc.instantiate(this.inputPwdPre);
            inputPwdPre.getComponent(inputPwdPre.name).init(this._bankCode,this.edMoney.string,this);
            canvas.addChild(inputPwdPre);

        }
    },

    onClose: function(){
        this._selectTg.isChecked =false;
        if(this._backState){  //输入金额成功后，按确定按钮
            this.KeysPre.getComponent(this.KeysPre.name).onClose();  //清除键盘界面
        }
        if(this.KeysPre){  
            this.KeysPre.getComponent(this.KeysPre.name).onClose();  //清除键盘界面 
        }  
        this.node.getComponent("Page").backAndRemove();
    },

    // update (dt) {},
});
